<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="校区名称" prop="campusName">
        <el-input v-model="queryParams.campusName" placeholder="请输入校区名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="详细地址" prop="campusAddress">
        <el-input v-model="queryParams.campusAddress" placeholder="请输入详细地址" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="负责人" prop="campusManager">
        <el-select v-model="queryParams.campusManager" placeholder="请输入负责人" >
    <el-option
      v-for="item in campusManagers"
      :key="item.userId"
      :label="item.nickname"
      :value="item.userId">
    </el-option>  
  </el-select>
  </el-form-item>
      <el-form-item label="状态" prop="campusStatus">
        <el-select v-model="queryParams.campusStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.sys_campus_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="推荐校区" prop="campusIsRecommended">
        <el-select v-model="queryParams.campusIsRecommended" placeholder="请选择推荐校区" clearable>
          <el-option v-for="dict in dict.type.sys_recommend_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="美术馆表单" prop="campusArtMenuEnabled">
        <el-select v-model="queryParams.campusArtMenuEnabled" placeholder="请选择美术馆表单" clearable>
          <el-option v-for="dict in dict.type.sys_recommend_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="主校区" prop="campusIsMain">
        <el-select v-model="queryParams.campusIsMain" placeholder="请选择主校区" clearable>
          <el-option v-for="dict in dict.type.sys_whether_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="上级校区" prop="campusParentName">
        <el-input v-model="queryParams.campusParentName" placeholder="请输入上级校区名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['campus:campusInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['campus:campusInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['campus:campusInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['campus:campusInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="campusInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="校区ID" align="center" prop="campusId" width="60" />
      <el-table-column label="校区名称" align="center" prop="campusName" width="150" />
      <el-table-column label="校区介绍" align="center" prop="campusIntroduction" width="150" />
      <el-table-column label="详细地址" align="center" prop="campusAddress" width="200" />
      <el-table-column label="负责人" align="center" prop="nickname" width="60" />
      <el-table-column label="联系电话" align="center" prop="campusPhone" width="150" />
      <el-table-column label="邮箱" align="center" prop="campusEmail" width="180" />
      <el-table-column label="状态" align="center" prop="campusStatus" width="70">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_campus_status" :value="scope.row.campusStatus" />
        </template>
      </el-table-column>
      <el-table-column label="推荐校区" align="center" prop="campusIsRecommended" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_recommend_status" :value="scope.row.campusIsRecommended" />
        </template>
      </el-table-column>
      <el-table-column label="美术馆表单" align="center" prop="campusArtMenuEnabled" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_recommend_status" :value="scope.row.campusArtMenuEnabled" />
        </template>
      </el-table-column>
      <el-table-column label="主校区" align="center" prop="campusIsMain" width="60">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_whether_status" :value="scope.row.campusIsMain" />
        </template>
      </el-table-column>
      <el-table-column label="校区收入" align="center" prop="campusIncome" width="80" />
      <el-table-column label="上级校区" align="center" prop="campusParentName" width="100" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">

          <!-- <el-dropdown>
            <el-button type="primary" size="mini">
              审核<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click="handleAudit('auditing')">审核中</el-dropdown-item>
              <el-dropdown-item @click="handleAudit('pass')">通过</el-dropdown-item>
              <el-dropdown-item @click="handleAudit('reject')">拒绝</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown> -->

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['campus:campusInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['campus:campusInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改校区列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="校区名称" prop="campusName">
          <el-input v-model="form.campusName" placeholder="请输入校区名称" />
        </el-form-item> -->
        <!-- 更改为下拉框选择校区 -->
        <el-form-item label="校区名称" prop="campusId">
          <el-select v-model="form.campusId" placeholder="请选择校区">
            <el-option v-for="campus in campusInfoList" :key="campus.campusId" :label="campus.campusName"
              :value="campus.campusId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="校区介绍" prop="campusIntroduction">
          <el-input v-model="form.campusIntroduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="校区图片" prop="campusLogoUrl">
          <el-upload class="upload-demo" drag action :multiple="false" :http-request="uploadImg"
            :show-file-list="false">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>

          <!-- 图片预览区域 -->
          <div v-if="form.campusLogoUrl" class="image-preview">
            <img :src="form.campusLogoUrl" alt="预览图" class="preview-image">
          </div>
        </el-form-item>

        <el-form-item label="详细地址" prop="campusAddress">
          <el-input v-model="form.campusAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="负责人" prop="campusManager">
        <el-select v-model="form.campusManager" placeholder="请输入负责人" >
    <el-option
      v-for="item in campusManagers"
      :key="item.userId"
      :label="item.nickname"
      :value="item.userId">
    </el-option>  
  </el-select>
  </el-form-item>
        <!-- <el-form-item label="负责人" prop="campusManager">
          <el-input v-model="form.campusManager" placeholder="请输入负责人" />
        </el-form-item> -->
        <el-form-item label="联系电话" prop="campusPhone">
          <el-input v-model="form.campusPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="campusEmail">
          <el-input v-model="form.campusEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="状态" prop="campusStatus">
          <el-select v-model="form.campusStatus" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.sys_campus_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推荐校区" prop="campusIsRecommended">
          <el-select v-model="form.campusIsRecommended" placeholder="请选择推荐校区">
            <el-option v-for="dict in dict.type.sys_recommend_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="美术馆表单" prop="campusArtMenuEnabled">
          <el-select v-model="form.campusArtMenuEnabled" placeholder="请选择美术馆表单">
            <el-option v-for="dict in dict.type.sys_recommend_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主校区" prop="campusIsMain">
          <el-select v-model="form.campusIsMain" placeholder="请选择主校区">
            <el-option v-for="dict in dict.type.sys_whether_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="上级校区" prop="campusParentName">
          <el-input v-model="form.campusParentId" placeholder="请输入上级校区名称" />
        </el-form-item> -->
        <!-- 更改为下拉框选择校区 -->
        <el-form-item label="上级校区名称" prop="campusParentId">
          <el-select v-model="form.campusParentId" placeholder="请选择上级校区">
            <el-option v-for="campus in campusInfoList" :key="campus.campusId" :label="campus.campusName"
              :value="campus.campusId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCampusInfo, getCampusInfo, delCampusInfo, addCampusInfo, updateCampusInfo,headmasterList } from "@/api/campus/campusInfo"
import { uploadIdCard} from "@/api/artGood/artGood"

export default {
  name: "CampusInfo",
  dicts: ['sys_whether_status', 'sys_recommend_status', 'sys_campus_status'],
  data() {
    return {
      // 负责人列表数据
      campusManagers: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 校区列表表格数据
      campusInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        campusName: null,
        campusAddress: null,
        campusManager: null,
        campusStatus: null,
        campusIsRecommended: null,
        campusArtMenuEnabled: null,
        campusIsMain: null,
        campusParentName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        campusName: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        campusManager: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        campusPhone: [
          { required: true, message: "不能为空", trigger: "blur" }
        ]
      },
      previewImage: '', // 图片预览URL
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询校区列表列表 */
    getList() {
      this.loading = true
      listCampusInfo(this.queryParams).then(response => {
        console.log(response,999999999999999)
        this.campusInfoList = response.rows
        this.total = response.total
        this.loading = false
      })
      headmasterList().then(response => {
        this.campusManagers = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        campusId: null,
        campusName: null,
        campusIntroduction: null,
        campusAddress: null,
        campusLogoUrl: null,
        campusManager: null,
        campusPhone: null,
        campusEmail: null,
        campusStatus: null,
        campusIsRecommended: null,
        campusArtMenuEnabled: null,
        campusIsMain: null,
        campusIncome: null,
        campusParentId: null,
        campusCreatedAt: null,
        campusUpdatedAt: null,
        demo1: null,
        demo2: null,
        demo3: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.campusId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加校区列表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const campusId = row.campusId || this.ids
      getCampusInfo(campusId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改校区列表"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.campusId != null) {
            updateCampusInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCampusInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const campusIds = row.campusId || this.ids
      this.$modal.confirm('是否确认删除校区列表编号为"' + campusIds + '"的数据项？').then(function () {
        return delCampusInfo(campusIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('campus/campusInfo/export', {
        ...this.queryParams
      }, `campusInfo_${new Date().getTime()}.xlsx`)
    },

    /* 按钮下拉框处理 */
    // handleClick() {
    //   alert('button click');
    // },
    handleAudit(type) {
      // 根据type的值（'auditing'、'pass'、'reject'等）执行相应的审核逻辑
      console.log('执行审核操作：', type);
    },
        uploadImg(item) {
      const formData = new FormData();
      formData.append("file", item.file);

      // 上传图片
      uploadIdCard(formData)
        .then(res => {
          this.$modal.msgSuccess("上传成功");
          // 直接赋值给表单的goodPicture字段
          this.form.goodPicture = res.data.file.url;
          console.log('图片URL已保存到表单:', this.form.goodPicture);

          // 可选：上传成功后显示预览
          this.previewImage = res.data.file.url;
        })
        .catch(error => {
          this.$modal.msgError("上传失败: " + error.message);
          console.error('图片上传失败:', error);
        });
    },
  }
}
</script>

<style scoped>
.el-dropdown {
  vertical-align: top;
}

.el-dropdown+.el-dropdown {
  margin-left: 15px;
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
